package com.cn.bulu.controller;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cn.bulu.dao.WorkerDao;
import com.cn.bulu.pojo.Customer;
import com.cn.bulu.service.CustomerService;
import com.cn.bulu.utils.BuluResult;
import com.cn.bulu.utils.HttpRequest;

/**
 * 短信验证码
 * @author wyw
 *
 */

@Controller

@RequestMapping("/phone")
public class PhoneVerification {
	@Resource
	private CustomerService customerService;
	@Resource
	private WorkerDao workerDao;
	
	//测试界面(跳到二维码)(顾客)
	
	@RequestMapping("/test")
	public String test(Integer customerId,Model model){
		model.addAttribute("customerId",customerId);
		return "phone/test";
	}
	//
	@RequestMapping("/test2")
	public String test2(Integer id,Model model){
		Customer customer=customerService.getCustomerById(id);
		model.addAttribute("customer",customer);
		return "phone/test2";
	}
	//通过订单号发送验证码
	@RequestMapping("sendMaByPhone")
	@ResponseBody
	public BuluResult sendMaByPhone(Integer indentId ,HttpServletRequest request){
//		String sign = request.getParameter("sign");
		String phone = workerDao.selectPhone(indentId);
		System.out.println(phone);
		Integer result = generateCode(phone,request);
		BuluResult buluResult = new BuluResult();
		if (result==1) {
			buluResult.setStatus(0);
//			buluResult.setData(redis.get(sign+"number"));
//			redis.set("indentId",String.valueOf(indentId));
		}else{
			//验证码发送失败
			buluResult.setStatus(1);
			buluResult.setMessage("发送失败,请重试!!!");
		}
		return buluResult;
	
	}
	public Integer generateCode(String phone,HttpServletRequest request){
//		redis.auth("sd3a3lkf@23I.N8");
		int number = (int)(Math.random()*(9999-1000+1))+1000;
    	String s=HttpRequest.sendGet("http://v1.avatardata.cn/Sms/Send", 
    			"key=d4bc2afdcc6d4231bd8f793d9ca6d694&mobile="+phone+"&templateId=4df4dbf1bce84f0ea6e7a8fd185d005e&param=鹿客注册系统&param="+number);
		System.out.println(s);
    	JSONObject parseObject = JSONObject.parseObject(s);
		String object = parseObject.getString("success");
		if (object.equals("true")) {
			Date beginTime = new Date();
			System.out.println(beginTime);
			//验证码发送成功
//			String sign = request.getParameter("sign");
//			redis.set(sign+"number", String.valueOf(number));
//			redis.set(sign+"beginTime", String.valueOf(beginTime.getTime()));
//			System.out.println(redis.get(sign+"beginTime"));
			return 1;
		}else{
			//验证码发送失败
			return 0;
		}
	}
	//对比验证码
	@RequestMapping("checkNumber")
	@ResponseBody
	public BuluResult checkNumber(Integer number ,HttpServletRequest request){
//		redis.auth("sd3a3lkf@23I.N8");
//		String sign = request.getParameter("sign");
		Date Time = new Date();
		Long overTime = Time.getTime();
		System.out.println(overTime);
//		System.out.println(redis.get(sign+"beginTime"));
//		Long beginTime = Long.parseLong(redis.get(sign+"beginTime"));
//		System.out.println(beginTime);
		BuluResult buluResult = new BuluResult();
//		System.out.println(overTime-beginTime);
//		if(overTime-beginTime<=12000){
//			String num = redis.get(sign+"number");
//			if(number.equals(num)){
//				buluResult.setStatus(0);
//			}else{
//				buluResult.setStatus(2);
//				buluResult.setMessage("验证码有误!!!");
//			}
//		}else{
//			buluResult.setStatus(1);
//			buluResult.setMessage("验证码已失效,请重新验证!!!");
//		}
//		Integer num=null;
//		if(request.getSession().getAttribute("number")!=null){
//			num=Integer.parseInt(request.getSession().getAttribute("number").toString());
//		}
//		if(number.equals(num)){
//			return 0;
//		}else{
//			return 1;
//		}
		return buluResult;
	}
	
	//注册
	@RequestMapping("customerRegister")
	@ResponseBody
	public Integer customerRegister(String phone,String referrer){
		Integer aInteger=customerService.InsertCustomerByPhone(phone,referrer);
		if (aInteger==1) {
			return 0;
		}else {
			return 1;
		}
		
	}
	//判断该手机号是否可以注册
	@RequestMapping("isPhone")
	@ResponseBody
	public BuluResult isPhone(String phone){
		BuluResult buluResult = customerService.isPhone(phone);
		return buluResult;
	}
	
}
